package com.bw.exam.controller;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.exam.pojo.Brand;
import com.bw.exam.pojo.TypeTemplate;
import com.bw.exam.pojo.response.R;
import com.bw.exam.service.TypeTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

/**
 * @author: liuzhiping
 * @date: 2024/7/18 15:06
 **/
@Slf4j
@RestController
@RequestMapping("/platform/typeTemplate")
public class TypeTemplateController {


    @Resource
    TypeTemplateService typeTemplateService;

    /**
     * 列表数据展示方法
     * @param typeTemplate
     * @return
     */
    @RequestMapping("list")
    public R list(@RequestBody TypeTemplate typeTemplate){

        log.info("typeTemplate:{}", JSON.toJSONString(typeTemplate));

        LambdaQueryWrapper<TypeTemplate> queryWarrper = new LambdaQueryWrapper<TypeTemplate>();
        if(StringUtils.isNotBlank(typeTemplate.getName())){
            queryWarrper.like(TypeTemplate::getName,typeTemplate.getName());
        }

        Page<TypeTemplate> pagedb = new Page<TypeTemplate>(typeTemplate.getStartPage(),typeTemplate.getEndPage());

        Page<TypeTemplate> pageResult = this.typeTemplateService.page(pagedb, queryWarrper);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",pageResult.getRecords());
        map.put("total",pageResult.getTotal());

        return R.success(map);
    }


    /**
     * 列表数据展示方法
     * @return
     */
    @RequestMapping("listAll")
    public R listAll(){
        return R.success(this.typeTemplateService.list());

    }


    @GetMapping("findById")
    public R findById(@RequestParam("id")Long id){

        return R.success(typeTemplateService.getById(id));
    }

    /**
     * 根据id进行删除
     * @param
     * @return
     */
    @RequestMapping("delById")
    public R delById(@RequestParam("id")Long id){
        return R.success(typeTemplateService.removeById(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("delByIds")
    public R delByIds(@RequestBody Long[] ids){
        log.info("=========>ids:"+JSON.toJSONString(ids));
        return R.success(typeTemplateService.removeByIds(Arrays.asList(ids)));
    }

    @RequestMapping("update")
    public R update(@RequestBody TypeTemplate typeTemplate){
        return R.success(typeTemplateService.saveOrUpdate(typeTemplate));
    }

    @RequestMapping("insert")
    public R insert(@RequestBody TypeTemplate typeTemplate){
        return R.success(typeTemplateService.save(typeTemplate));
    }

}

